package vip.xiaonuo.smzq.modular.lifecycle.service.module.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.smzq.modular.kcl.service.IZbService;
import vip.xiaonuo.smzq.modular.lifecycle.constant.LifeCycleConstant;
import vip.xiaonuo.smzq.modular.lifecycle.entity.*;
import vip.xiaonuo.smzq.modular.lifecycle.entity.view.GpKsMaxYear;
import vip.xiaonuo.smzq.modular.lifecycle.funcEnum.FuncEnum;
import vip.xiaonuo.smzq.modular.lifecycle.mapper.entity.KclKsMapper;
import vip.xiaonuo.smzq.modular.lifecycle.mapper.entity.LifeCycleMineStoreMapper;
import vip.xiaonuo.smzq.modular.lifecycle.mapper.entity.LifeCycleMineralMeaMapper;
import vip.xiaonuo.smzq.modular.lifecycle.mapper.module.CommonMapper;
import vip.xiaonuo.smzq.modular.lifecycle.mapper.module.LifeCycleMapper;
import vip.xiaonuo.smzq.modular.lifecycle.po.QueryAdministractiveRegionPO;
import vip.xiaonuo.smzq.modular.lifecycle.po.QueryMineInfoPO;
import vip.xiaonuo.smzq.modular.lifecycle.po.QueryMineStorePO;
import vip.xiaonuo.smzq.modular.lifecycle.po.QueryYearStorePO;
import vip.xiaonuo.smzq.modular.lifecycle.po.datamanager.QueryMineListPO;
import vip.xiaonuo.smzq.modular.lifecycle.service.entity.HisiKsService;
import vip.xiaonuo.smzq.modular.lifecycle.service.entity.KclCb08XzqhService;
import vip.xiaonuo.smzq.modular.lifecycle.service.entity.KclKsService;
import vip.xiaonuo.smzq.modular.lifecycle.service.entity.KclZbService;
import vip.xiaonuo.smzq.modular.lifecycle.service.module.CommonService;
import vip.xiaonuo.smzq.modular.lifecycle.service.module.LifeCycleService;
import vip.xiaonuo.smzq.modular.lifecycle.util.CoordinateTranUtil;
import vip.xiaonuo.smzq.modular.lifecycle.util.HutoolTreeExtend;
import vip.xiaonuo.smzq.modular.lifecycle.vo.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <ul>
 * <li>name:  LifeCycleServiceImpl</li>
 * <li>author name: GEOSCENE_YUYANFEI</li>
 * <li>create time: 2024-03-20 15:31:25</li>
 * </ul>
 */
@Service
public class LifeCycleServiceImpl implements LifeCycleService {

    @Autowired
    CommonMapper commonMapper;

    @Autowired
    IZbService iZbService;

    @Autowired
    KclKsMapper kclKsMapper;

    @Autowired
    LifeCycleMineStoreMapper lifeCycleMineStoreMapper;

    @Autowired
    LifeCycleMapper lifeCycleMapper;

    @Autowired
    LifeCycleMineralMeaMapper lifeCycleMineralMeaMapper;

    @Autowired
    KclKsService kclKsService;

    @Autowired
    KclCb08XzqhService kclCb08XzqhService;

    @Autowired
    CommonService commonService;

    @Resource
    HutoolTreeExtend hutoolTreeExtend;

    @Resource
    CoordinateTranUtil coordinateTranUtil;

    @Autowired
    HisiKsService hisiKsService;

    @Autowired
    KclZbService kclZbService;



    @Override
    public List<QueryMineInfoRetVO> queryMineInfo(QueryMineInfoPO queryMineInfoPO) {
        List<QueryMineInfoRetVO> result = CollUtil.newArrayList();

        List<List<QueryMineInfoVO>> mineObligeeTypeGrp =  CollUtil.groupByField(queryMineInfoList(queryMineInfoPO,true), "mineObligeeType");
        one:
        for (String mineType:LifeCycleConstant.mineTypes) {
            QueryMineInfoRetVO queryMineInfoRetVO = new QueryMineInfoRetVO();
            queryMineInfoRetVO.setMineObligeeType(mineType);
            two:
            for (List<QueryMineInfoVO> mineObligeeTypeList:mineObligeeTypeGrp) {
                if(StrUtil.equals(mineType, CollUtil.getFirst(mineObligeeTypeList).getMineObligeeType())){
                    queryMineInfoRetVO.setQueryMineInfoVOS(mineObligeeTypeList);
                    break two;
                }
            }

            result.add(queryMineInfoRetVO);
        }

        return result;
    }

    /**
     * 根据许可证号获取矿山最新年度
     * @param licenseNum
     * @return
     */
    private Integer getNewestYearByLicenseNum(String licenseNum){
        Integer result = null;
        KclKs kclKs = new KclKs();
        kclKs.setXkzh(licenseNum);
        List<KclKs> kclKsList =  kclKsService.queryKclKsList(kclKs);
        if(CollUtil.isNotEmpty(kclKsList)){
            result = CollUtil.getFirst(kclKsList).getNd();
        }
        return result;
    }

    /**
     * 得到最新年度
     * @return
     */
    private Integer getNewestYear(){
        Integer result = 2023;
        return result;
    }

    @Override
    public List<QueryMineInfoVO> queryMineInfoList(QueryMineInfoPO queryMineInfoPO,boolean isQuerySW) {
        Integer year = null;
//        if(ObjectUtil.isNotEmpty(queryMineInfoPO.getYear())){
//            year = queryMineInfoPO.getYear();
//        }else{
//        if(ObjectUtil.isNotEmpty(queryMineInfoPO.getNd())){
//            year = queryMineInfoPO.getNd();
//        }else{
        if(StrUtil.isNotBlank(queryMineInfoPO.getLicenseNum())){
            year = getNewestYearByLicenseNum(queryMineInfoPO.getLicenseNum());
        }else{
            year = getNewestYear();
        }
//        }

//        }


        //查询矿山，探矿权类型，以及坐标信息
        List<QueryMineInfoVO> queryMineInfoVOS = kclKsMapper.selectJoinList(QueryMineInfoVO.class
                ,new MPJLambdaWrapper<KclKs>()
                        .selectAll(KclKs.class)
                        .selectAs(KclKs::getPkid, QueryMineInfoVO::getMineStoreId)
//                        矿区坐标
                        .selectAs(KclZb::getKqzb, QueryMineInfoVO::getDmlCoor)
                        .selectAs(KclZb::getJszb, QueryMineInfoVO::getCalCoor)
                        .selectAs(KclZb::getDlzb, QueryMineInfoVO::getMomCoor)
                        .selectAs(KclCb08Xzqh::getMc, QueryMineInfoVO::getRegionName)
                        .leftJoin(KclZb.class,"kclZb",on->
                                on.eq(KclZb::getNd,KclKs::getNd)
                                        .eq(KclZb::getDjflbh,KclKs::getDjflbh)
                                        .eq(KclZb::getKqbh, KclKs::getKqbh)
                        )
                        .ge(KclKs::getNd,year)
                        .innerJoin(GpKsMaxYear.class,"gp_ks_max_year", on->
                                on.eq(GpKsMaxYear::getMaxnd, KclKs::getNd)
                                .eq(GpKsMaxYear::getKqbh, KclKs::getKqbh))
                        .eq(GpKsMaxYear::getDjflbh, KclKs::getDjflbh)
//                                        .apply(getKsDjflbhLastThreeLSQL("t"))

//                        .leftJoin(LifeCycleMineExtend.class, LifeCycleMineExtend::getMineId,KclKs::getPkid)
                        .leftJoin(KclCb08Xzqh.class,KclCb08Xzqh::getDm,KclKs::getXzqdm)
                        .leftJoin(KclKsjj.class,"kclksjj",on->
                                on.eq(KclKsjj::getDjflbh,KclKs::getDjflbh)
                                .eq(KclKsjj::getKqbh,KclKs::getKqbh)
                                .eq(KclKsjj::getNd, KclKs::getNd)
                        )

                        .eq(StrUtil.isNotBlank(queryMineInfoPO.getOomrId()), KclKs::getKyqr, queryMineInfoPO.getOomrId())
                        .eq(StrUtil.isNotBlank(queryMineInfoPO.getLicenseNum()), KclKs::getXkzh, queryMineInfoPO.getLicenseNum())
                        .eq(ObjectUtil.isNotEmpty(queryMineInfoPO.getZkcm()), KclKs::getZkcm, queryMineInfoPO.getZkcm())
                        .eq(ObjectUtil.isNotEmpty(queryMineInfoPO.getXkzh()), KclKs::getXkzh, queryMineInfoPO.getXkzh())
//                        .eq(ObjectUtil.isNotEmpty(queryMineInfoPO.getNd()), KclKs::getNd, queryMineInfoPO.getNd())
                        .eq(ObjectUtil.isNotEmpty(queryMineInfoPO.getDjflbh()), KclKs::getDjflbh, queryMineInfoPO.getDjflbh())
                        .eq(ObjectUtil.isNotEmpty(queryMineInfoPO.getKqbh()), KclKs::getKqbh, queryMineInfoPO.getKqbh())
                        .in(CollUtil.isNotEmpty(queryMineInfoPO.getSizes()),KclKsjj::getKsgm, queryMineInfoPO.getSizes())
//                        .eq(ObjectUtil.isNotNull(year), KclKs::getNd,year)
                        .in(CollUtil.isNotEmpty(queryMineInfoPO.getAreaCodes()),KclKs::getXzqdm, queryMineInfoPO.getAreaCodes())
                        .like(StrUtil.isNotBlank(queryMineInfoPO.getKeyword()),KclKs::getKsmc, queryMineInfoPO.getKeyword())
//                        .in(KclKs::getPkid, CollUtil.newArrayList("1781943632880361480","1781943632888750082"))
//                        .eq(KclKs::getNd,queryMineInfoPO.getYear())

        );

        //根据矿山id去重
        List<QueryMineInfoVO> resQueryMineInfoVOS = CollUtil.newArrayList();
        for (QueryMineInfoVO queryMineInfoVO:queryMineInfoVOS) {
            if(!checkQuerymfHas(resQueryMineInfoVOS,queryMineInfoVO.getPkid())){
                resQueryMineInfoVOS.add(queryMineInfoVO);
            }
        }

        initMineCool(resQueryMineInfoVOS,isQuerySW);
        initMineType(resQueryMineInfoVOS);
//        过滤开采状态
        if(CollUtil.isNotEmpty(queryMineInfoPO.getExpStatus())){
            resQueryMineInfoVOS = resQueryMineInfoVOS.stream().filter(ele->CollUtil.contains(queryMineInfoPO.getExpStatus(), ele.getKsType())).collect(Collectors.toList());
//            resQueryMineInfoVOS = resQueryMineInfoVOS.stream().filter(ele->StrUtil.equals(ele.getKsType(),queryMineInfoPO.getExpStatus())).collect(Collectors.toList());
        }

        return fromTKQCopyToWKQ(getKsExportType(resQueryMineInfoVOS));
    }

    /**
     *
     * @param ksAlias
     * @return
     */
    @Override
    public String getKsDjflbhLastThreeLSQL(String ksAlias){
        return "substring("+ksAlias+".djflbh::character varying::text, length("+ksAlias+".djflbh::character varying::text) - 2, 3) = gp_ks_max_year.reglastthree";
    }

    /**
     * 初始化矿山类别
     * @param queryMineInfoVOS 矿山信息
     */
    public void initMineType(List<QueryMineInfoVO> queryMineInfoVOS){
        queryMineInfoVOS.forEach(ele->{
            ele.initKsType();
            /*String regClassCode = Convert.toStr(ele.getDjflbh());
            if(StrUtil.equals("1000",regClassCode)){
                ele.setKsmc(ele.getKsmc()+getBrkStr(LifeCycleConstant.wly));
            }else if(StrUtil.startWith(regClassCode,"2")){
                ele.setKsmc(ele.getKsmc()+getBrkStr(LifeCycleConstant.sc));
            }else if(StrUtil.startWith(regClassCode,"3")){
                ele.setKsmc(ele.getKsmc()+getBrkStr(LifeCycleConstant.gb));
            }else if(StrUtil.startWith(regClassCode,"4")){
                ele.setKsmc(ele.getKsmc()+getBrkStr(LifeCycleConstant.yf));
            }*/
        });
    }

    /**
     * 得到括号字符串
     * @return 括号字符串
     */
    public String getBrkStr(String str){
        return "("+str+")";
    }

    @Override
    public void initMineCool(List<QueryMineInfoVO> queryMineInfoVOS,boolean isQuerySW){
        queryMineInfoVOS.forEach(ele->{
//            设置矿区范围
            if(checkCoorTrue(ele.getDmlCoor())){
                PolygonWgs84V0 polygonWgs84V0 = coordinateTranUtil.coordinateDisposeWKT3(ele.getDmlCoor());
                ele.setWkt3MineRightCoordinate(polygonWgs84V0.getMultiPolygon());
                ele.setWkt3MineRightCoordinateHole(polygonWgs84V0.getHolePolygon());
//                设置三维显示的高信息
                if(isQuerySW){
                    ele.setThreeWeftCoorDmlVOS(commonService.getThreeWeftCoorVOS(ele.getDmlCoor()));
//                    ele.setThreeDHeightDmlVOS(commonService.getThreeDHeightVOS(ele.getDmlCoor()));
                }

            }
            //            设置计算范围
            if(checkCoorTrue(ele.getCalCoor())){
                PolygonWgs84V0 polygonWgs84V0 = coordinateTranUtil.coordinateDisposeWKT3(ele.getCalCoor());
                ele.setCalWkt3MineRightCoordinate(polygonWgs84V0.getMultiPolygon());
                ele.setCalWkt3MineRightCoordinateHole(polygonWgs84V0.getHolePolygon());
                if(isQuerySW){
                    ele.setThreeWeftCoorCalVOS(commonService.getThreeWeftCoorVOS(ele.getCalCoor()));
//                    ele.setThreeDHeightCalVOS(commonService.getThreeDHeightVOS(ele.getCalCoor()));
                }
            }

//            设置动量范围
            if(checkCoorTrue(ele.getMomCoor())){
                PolygonWgs84V0 polygonWgs84V0 = coordinateTranUtil.coordinateDisposeWKT3(ele.getMomCoor());
                ele.setMomWkt3MineRightCoordinate(polygonWgs84V0.getMultiPolygon());
                ele.setMomWkt3MineRightCoordinateHole(polygonWgs84V0.getHolePolygon());
                if(isQuerySW){
                    ele.setThreeWeftCoorMomVOS(commonService.getThreeWeftCoorVOS(ele.getMomCoor()));
//                    ele.setThreeDHeightMomVOS(commonService.getThreeDHeightVOS(ele.getMomCoor()));
                }
            }

            if(StrUtil.isNotBlank(ele.getZxdxzb()) && StrUtil.isNotBlank(ele.getZxdyzb())){
                if(checkCoorIs84(ele.getZxdxzb(),ele.getZxdyzb())){
                    ele.setMineCenter84X(coordinateTranUtil.dmsToWgs84(Convert.toDouble(ele.getZxdyzb())));
                    ele.setMineCenter84Y(coordinateTranUtil.dmsToWgs84(Convert.toDouble(ele.getZxdxzb())));
                }else {
                    List<Double> xyCoor= coordinateTranUtil.get84Coordinates(ele.getZxdxzb(),ele.getZxdyzb());
                    ele.setMineCenter84X(CollUtil.getFirst(xyCoor));
                    ele.setMineCenter84Y(CollUtil.getLast(xyCoor));
                }

            }

        });
    }

    private boolean checkPedPidsIsAllMainMine(List<String> pedPids,Map<String,LifeCyclePedigee> allPedMap){
        boolean result = true;

        for (String pedPid:pedPids) {
            LifeCyclePedigee lifeCyclePedigee = allPedMap.get(pedPid);
            if(ObjectUtil.isEmpty(lifeCyclePedigee)){
                result = false;
                break;
            }else{
                if(ObjectUtil.isNotEmpty(lifeCyclePedigee.getMainMine())){
                    if(!lifeCyclePedigee.getMainMine()){
                        result = false;
                        break;
                    }

                }
            }


        }

        return result;
    }



    /**
     * 获取别的矿山的生命周期节点
     * @return
     */
    @Override
    public List<QueryMineStoreVO> queryOtherMineLcNode(Map<String,LifeCyclePedigee> curAllPedMap,List<List<String>> megerPedIdsList){
//        别的开矿山的族谱id列表
        List<String> otherMinePedIds = CollUtil.newArrayList();
        for (List<String> megerPedIds:megerPedIdsList) {
            for (String megerPedId:megerPedIds) {
//                如果为空，则说明是别的矿山的族谱数据
                if(ObjectUtil.isEmpty(curAllPedMap.get(megerPedId))){
                    otherMinePedIds.add(megerPedId);
                }
            }
        }
        if(CollUtil.isNotEmpty(otherMinePedIds)){
            QueryMineStorePO queryMineStorePO = new QueryMineStorePO();
            queryMineStorePO.setPedIds(otherMinePedIds);
            List<QueryMineStoreVO> queryMineStoreVOS = lifeCycleMineStoreMapper.selectJoinList(QueryMineStoreVO.class,getQueryLcBaseWrapper(queryMineStorePO,false,true));
            queryMineStoreVOS.forEach(ele->{
                ele.initOtherAll();
                ele.initPedSec();
            });
//            初始化生命周期族谱坐标
            initLcCoor(queryMineStoreVOS);
            return queryMineStoreVOS;
        }
        return CollUtil.newArrayList();
    }

    @Override
    public MPJLambdaWrapper<LifeCycleMineStore> getQueryLcBaseWrapper(QueryMineStorePO queryMineStorePO,boolean isMustMineralCode,boolean isConMine) {

        boolean isValidYearReport = CollUtil.contains(queryMineStorePO.getReportNames(),"年报") || CollUtil.isEmpty(queryMineStorePO.getReportNames());

        MPJLambdaWrapper<LifeCycleMineStore> wrapper = new MPJLambdaWrapper<LifeCycleMineStore>()
                .selectAll(LifeCycleMineStore.class)
                .select(KclClsp::getSpzt)
                .selectAs(LifeCycleMineralMea::getStoreVal,QueryMineStoreVO::getStoreVal)
                .selectCollection(KclWjglb.class, QueryMineStoreVO::getKclWjglbList)
                .selectCollection(LifeCycleMineralMea.class, QueryMineStoreVO::getLifeCycleMineralMeaList)
                .selectCollection(LifeCyclePedigee.class, QueryMineStoreVO::getLifeCyclePedigeeList);
                if(isConMine){
                    wrapper.select(KclKs::getZkcm)
                    .leftJoin(KclKs.class,on->
                            on.eq(KclKs::getDjflbh, LifeCycleMineStore::getRegClassCode)
                            .eq(KclKs::getNd, LifeCycleMineStore::getYear)
                            .eq(KclKs::getKqbh, LifeCycleMineStore::getMineralCode)
                    );
                }
                wrapper.leftJoin(LifeCycleMineralMea.class, LifeCycleMineralMea::getMineStoreId, LifeCycleMineStore::getId)
                .leftJoin(KclWjglb.class, on->
                        on.eq(KclWjglb::getZlscId,LifeCycleMineStore::getFileId)
                                .or()
                                .eq(KclWjglb::getZlscId,LifeCycleMineStore::getId)
                )
                .leftJoin(LifeCyclePedigee.class, LifeCyclePedigee::getMineStoreId, LifeCycleMineStore::getId)
                .leftJoin(KclClsp.class,on->
                        on.eq(KclClsp::getNd,LifeCycleMineStore::getYear)
                        .eq(KclClsp::getDjflbm, LifeCycleMineStore::getRegClassCode)
                        .eq(KclClsp::getKqbh,LifeCycleMineStore::getMineralCode)
                        )

                .eq(StrUtil.isNotBlank(queryMineStorePO.getId()),LifeCycleMineStore::getId,queryMineStorePO.getId())
                .eq(StrUtil.isNotBlank(queryMineStorePO.getOomrId()),KclKs::getKyqr, queryMineStorePO.getOomrId())
//                将这个矿山id换成，通过等级分类编号的后两位和矿区编号查询
//                .eq(LifeCycleMineStore::getMineralCode,queryMineStorePO.getMineralCode())
                .le(ObjUtil.isNotNull(queryMineStorePO.getEndDate()),LifeCycleMineStore::getStoreDate,Convert.toLocalDateTime(queryMineStorePO.getEndDate()))
                .ge(ObjUtil.isNotNull(queryMineStorePO.getStartDate()),LifeCycleMineStore::getStoreDate,Convert.toLocalDateTime(queryMineStorePO.getStartDate()))
                .in(CollUtil.isNotEmpty(queryMineStorePO.getReportNames()),LifeCycleMineStore::getReportName,queryMineStorePO.getReportNames())

                        //                        不等于年报
//                        .ne(LifeCycleMineStore::getReportName,"年报")
                        /*.or(wr2->wr2
                                .eq(LifeCycleMineStore::getReportName,"年报")
                                .eq(KclClsp::getSpzt,"5")
                        )*/
                        /*.or(isValidYearReport,wr->
                                //或者是年报，在22年之前的
                                wr.eq(LifeCycleMineStore::getReportName,"年报")
                                .le(LifeCycleMineStore::getYear,2022)

//                或者是年报已经审批过的
                                .or(wr2->wr2
                                        .eq(LifeCycleMineStore::getReportName,"年报")
                                        .eq(KclClsp::getSpzt,"5")
                                )
                        )
*/
                .eq(LifeCycleMineStore::getApproveStatus, 1)
                .eq(ObjectUtil.isNotEmpty(queryMineStorePO.getCpTkqToWkq()), LifeCycleMineStore::getCpTkqToWkq,queryMineStorePO.getCpTkqToWkq())
                .in(CollUtil.isNotEmpty(queryMineStorePO.getPedIds()), LifeCyclePedigee::getId,queryMineStorePO.getPedIds());
        if(isMustMineralCode){
            wrapper.eq(StrUtil.isNotBlank(queryMineStorePO.getMineralCode()),LifeCycleMineStore::getMineralCode,queryMineStorePO.getMineralCode());
        }else{
            wrapper.eq(StrUtil.isNotBlank(queryMineStorePO.getMineralCode()),LifeCycleMineStore::getMineralCode,queryMineStorePO.getMineralCode());
        }

        return wrapper;
    }

    @Override
    public MineCoordinateVO getLcNodCoor(LifeCycleMineStore lifeCycleMineStore) {
        if(ObjectUtil.isEmpty(lifeCycleMineStore)){
            return new MineCoordinateVO();
        }
        MineCoordinateVO mineCoordinateVO = BeanUtil.copyProperties(lifeCycleMineStore,MineCoordinateVO.class);
//        如果是年报
        if(StrUtil.equals(lifeCycleMineStore.getReportName(),"年报")){
//            查询年报坐标
            List<KclZb> kclZbs = kclZbService.lambdaQuery()
                    .eq(KclZb::getNd, lifeCycleMineStore.getYear())
                    .eq(KclZb::getDjflbh, lifeCycleMineStore.getRegClassCode())
                    .eq(KclZb::getKqbh, lifeCycleMineStore.getMineralCode()).list();
            if(CollUtil.size(kclZbs)!=0){
                KclZb kclZb = CollUtil.getFirst(kclZbs);
                if(StrUtil.isBlank(mineCoordinateVO.getDmlCoor())){
                    mineCoordinateVO.setDmlCoor(kclZb.getKqzb());
                }

                if(StrUtil.isBlank(mineCoordinateVO.getCalCoor())){
                    mineCoordinateVO.setCalCoor(kclZb.getJszb());
                }

                if(StrUtil.isBlank(mineCoordinateVO.getMomCoor())){
                    mineCoordinateVO.setMomCoor(kclZb.getDlzb());
                }
            }
        }



        if(StrUtil.isNotBlank(mineCoordinateVO.getDmlCoor())){
            PolygonWgs84V0 polygonWgs84V0 = coordinateTranUtil.coordinateDisposeWKT3(mineCoordinateVO.getDmlCoor());
            mineCoordinateVO.setDmlCoordiate84(polygonWgs84V0.getMultiPolygon());
            mineCoordinateVO.setDmlCoordiate84Hole(polygonWgs84V0.getHolePolygon());
            mineCoordinateVO.setThreeWeftCoorDmlVOS(commonService.getThreeWeftCoorVOS(mineCoordinateVO.getDmlCoor()));
        }

        if(StrUtil.isNotBlank(mineCoordinateVO.getCalCoor())){
            PolygonWgs84V0 polygonWgs84V0 = coordinateTranUtil.coordinateDisposeWKT3(mineCoordinateVO.getCalCoor());
            mineCoordinateVO.setCalCoordiate84(polygonWgs84V0.getMultiPolygon());
            mineCoordinateVO.setCalCoordiate84Hole(polygonWgs84V0.getHolePolygon());
            mineCoordinateVO.setThreeWeftCoorCalVOS(commonService.getThreeWeftCoorVOS(mineCoordinateVO.getCalCoor()));
        }


        if(StrUtil.isNotBlank(mineCoordinateVO.getMomCoor())){
            PolygonWgs84V0 polygonWgs84V0 = coordinateTranUtil.coordinateDisposeWKT3(mineCoordinateVO.getMomCoor());
            mineCoordinateVO.setMomCoordiate84(polygonWgs84V0.getMultiPolygon());
            mineCoordinateVO.setMomCoordiate84Hole(polygonWgs84V0.getHolePolygon());
            mineCoordinateVO.setThreeWeftCoorMomVOS(commonService.getThreeWeftCoorVOS(mineCoordinateVO.getMomCoor()));
        }

        if(StrUtil.isNotBlank(mineCoordinateVO.getExpCoor())){
            PolygonWgs84V0 polygonWgs84V0 = coordinateTranUtil.coordinateDisposeWKT3(mineCoordinateVO.getExpCoor());
            mineCoordinateVO.setExpCoordiate84(polygonWgs84V0.getMultiPolygon());
            mineCoordinateVO.setExpCoordiate84Hole(polygonWgs84V0.getHolePolygon());
            mineCoordinateVO.setThreeWeftCoorExpVOS(commonService.getThreeWeftCoorVOS(mineCoordinateVO.getExpCoor()));
        }

        if(StrUtil.isNotBlank(mineCoordinateVO.getPreCovCoor())){
            PolygonWgs84V0 polygonWgs84V0 = coordinateTranUtil.coordinateDisposeWKT3(mineCoordinateVO.getPreCovCoor());
            mineCoordinateVO.setPreCovCoordiate84(polygonWgs84V0.getMultiPolygon());
            mineCoordinateVO.setPreCovCoordiate84Hole(polygonWgs84V0.getHolePolygon());
            mineCoordinateVO.setThreeWeftCoorPreCovVOS(commonService.getThreeWeftCoorVOS(mineCoordinateVO.getPreCovCoor()));
        }

        if(StrUtil.isNotBlank(mineCoordinateVO.getExpMinCoor())){
            PolygonWgs84V0 polygonWgs84V0 = coordinateTranUtil.coordinateDisposeWKT3(mineCoordinateVO.getExpMinCoor());
            mineCoordinateVO.setExpMinCoordiate84(polygonWgs84V0.getMultiPolygon());
            mineCoordinateVO.setExpMinCoordiate84Hole(polygonWgs84V0.getHolePolygon());
            mineCoordinateVO.setThreeWeftCoorExpMinVOS(commonService.getThreeWeftCoorVOS(mineCoordinateVO.getExpMinCoor()));
        }
        return mineCoordinateVO;
    }


    @Override
    public List<QueryMineStoreVO> handerMerge(List<QueryMineStoreVO> queryMineStoreVOS) {
//        合并的id列表
        List<List<String>> mergeIdLists = CollUtil.newArrayList();
        List<LifeCyclePedigee> allPed = CollUtil.newArrayList();

//        List<List<String>> mainSecMineMergeList = CollUtil.newArrayList();

        for (QueryMineStoreVO queryMineStoreVO:queryMineStoreVOS) {
            CollUtil.addAll(allPed,queryMineStoreVO.getLifeCyclePedigeeList());
        }
        allPed = CollUtil.distinct(allPed,LifeCyclePedigee::getId,true);
        Map<String,LifeCyclePedigee> allPedMap = allPed.stream().collect(Collectors.toMap(LifeCyclePedigee::getId, Function.identity()));
//        先找出所有的合并的数据


        for (QueryMineStoreVO queryMineStoreVO:queryMineStoreVOS) {
            List<LifeCyclePedigee> lifeCyclePedigees = queryMineStoreVO.getLifeCyclePedigeeList();
            for (LifeCyclePedigee lifeCyclePedigee:lifeCyclePedigees) {
                List<String> parentPedIds = StrUtil.split(lifeCyclePedigee.getParentId(),",");
//                如果是合并
                if(CollUtil.size(parentPedIds)>1){
                    if(checkPedPidsIsAllMainMine(parentPedIds,allPedMap)){
                        for (String parentPedId:parentPedIds) {
                            mergeIdLists.add(CollUtil.newArrayList(parentPedId));
                        }
                    }else{
                        mergeIdLists.add(parentPedIds);
//                        mainSecMineMergeList.add(parentPedIds);
                    }
                }
            }
        }

        List<QueryMineStoreVO> oterMineLcNodes = queryOtherMineLcNode(allPedMap,mergeIdLists);
        CollUtil.addAll(queryMineStoreVOS,oterMineLcNodes);
//        合并的生命周期节点id列表
        List<String> megerLcIds = CollUtil.newArrayList();

        List<HanderMegerVO> handerMegerVOS = CollUtil.newArrayList();

        for (List<String> mergeIds:mergeIdLists) {
            HanderMegerVO handerMegerVO = new HanderMegerVO();
//            操作合并的vo类
            for (QueryMineStoreVO queryMineStoreVO:queryMineStoreVOS) {

                List<LifeCyclePedigee> lifeCyclePedigees = queryMineStoreVO.getLifeCyclePedigeeList();
                for (LifeCyclePedigee lifeCyclePedigee:lifeCyclePedigees) {
//                    如果族谱是合并
                    if(CollUtil.contains(mergeIds,lifeCyclePedigee.getId())){

                        if(!CollUtil.contains(megerLcIds,queryMineStoreVO.getId())){
                            megerLcIds.add(queryMineStoreVO.getId());
                        }
                        if(lifeCyclePedigee.getMainMine()){
                            handerMegerVO.setMainMineLc(queryMineStoreVO);
                        }else{
                            handerMegerVO.setSecPedList(queryMineStoreVO.getLifeCyclePedigeeList());
                        }

                    }
                }


            }

            if(ObjectUtil.isNotEmpty(handerMegerVO.getMainMineLc())){
                handerMegerVOS.add(handerMegerVO);
            }

        }

        //        删除合并的所有生命周期节点
        List<QueryMineStoreVO> filterQmsVOS = queryMineStoreVOS.stream().filter(ele->!CollUtil.contains(megerLcIds,ele.getId())).collect(Collectors.toList());

//将次矿山族谱，加到主矿山族谱列表中
        for (HanderMegerVO handerMegerVO:handerMegerVOS) {

            if(ObjectUtil.isNotEmpty(handerMegerVO.getMainMineLc())){
                QueryMineStoreVO queryMineStoreVO = handerMegerVO.getMainMineLc();
                List<LifeCyclePedigee> mainPedList = queryMineStoreVO.getLifeCyclePedigeeList();
                List<LifeCyclePedigee> seclifeCyclePedigees = handerMegerVO.getSecPedList();
//                如果次矿山不为空
                if(CollUtil.isNotEmpty(seclifeCyclePedigees)){
                    CollUtil.addAll(mainPedList,seclifeCyclePedigees);
//                    CollUtil.addAll(seclifeCyclePedigees,mainPedList);
                    queryMineStoreVO.setLifeCyclePedigeeList(mainPedList);
//                    queryMineStoreVO.setLifeCyclePedigeeList(seclifeCyclePedigees);
                }else{
                    queryMineStoreVO.setLifeCyclePedigeeList(mainPedList);
                }

                filterQmsVOS.add(queryMineStoreVO);
            }

        }
        List<QueryMineStoreVO> result = CollUtil.sortByProperty(filterQmsVOS,"storeDate");
        return CollUtil.distinct(result);
    }


    /*public void initMineCool_bak(List<QueryMineInfoVO> queryMineInfoVOS,boolean isQuerySW){
        queryMineInfoVOS.forEach(ele->{
//            设置矿区范围
            if(checkCoorTrue(ele.getDmlCoor())){
                ele.setWkt3MineRightCoordinate(coordinateTranUtil.coordinateDisposeWKT3(ele.getDmlCoor()));
//                设置三维显示的高信息
                if(isQuerySW){

                    ele.setThreeDHeightDmlVOS(commonService.getThreeDHeightVOS(ele.getDmlCoor()));
                }

            }
            //            设置计算范围
            if(checkCoorTrue(ele.getCalCoor())){
                ele.setCalWkt3MineRightCoordinate(coordinateTranUtil.coordinateDisposeWKT3(ele.getCalCoor()));
                if(isQuerySW){
                    ele.setThreeDHeightCalVOS(commonService.getThreeDHeightVOS(ele.getCalCoor()));
                }
            }

//            设置动量范围
            if(checkCoorTrue(ele.getMomCoor())){
                ele.setMomWkt3MineRightCoordinate(coordinateTranUtil.coordinateDisposeWKT3(ele.getMomCoor()));
                if(isQuerySW){
                    ele.setThreeDHeightMomVOS(commonService.getThreeDHeightVOS(ele.getMomCoor()));
                }
            }

            if(StrUtil.isNotBlank(ele.getZxdxzb()) && StrUtil.isNotBlank(ele.getZxdyzb())){
                if(checkCoorIs84(ele.getZxdxzb(),ele.getZxdyzb())){
                    ele.setMineCenter84X(coordinateTranUtil.dmsToWgs84(Convert.toDouble(ele.getZxdyzb())));
                    ele.setMineCenter84Y(coordinateTranUtil.dmsToWgs84(Convert.toDouble(ele.getZxdxzb())));
                }else {
                    List<Double> xyCoor= coordinateTranUtil.get84Coordinates(ele.getZxdxzb(),ele.getZxdyzb());
                    ele.setMineCenter84X(CollUtil.getFirst(xyCoor));
                    ele.setMineCenter84Y(CollUtil.getLast(xyCoor));
                }

            }

        });
    }*/

//    检查坐标是否正确
    private boolean checkCoorTrue(String coor){
        return StrUtil.isNotBlank(coor) && !StrUtil.equals(coor,"0,");
    }

    /**
     * 得到矿山矿业权类型，以2开头的为采矿权 以 1开头的xkzh不为空的则为探矿权  如果是3和4 则则同一个矿区的 看1的
     * @param queryMineInfoVOS
     * @return
     */
    @Override
    public List<QueryMineInfoVO> getKsExportType(List<QueryMineInfoVO> queryMineInfoVOS){
        List<QueryMineInfoVO> result = CollUtil.newArrayList();
        List<String> mineralCodes = queryMineInfoVOS.stream().map(ele->ele.getKqbh()).distinct().collect(Collectors.toList());
//        矿区字典，key为矿区码，value为该矿区对应的矿山
        Dict mineralDict = Dict.create();
        for (String mineralCode:mineralCodes) {
            List<QueryMineInfoVO> mineralMineList = CollUtil.newArrayList();
            for (QueryMineInfoVO queryMineInfoVO:queryMineInfoVOS) {
                if(StrUtil.equals(mineralCode, queryMineInfoVO.getKqbh())){
                    mineralMineList.add(queryMineInfoVO);
                }
            }
            mineralDict.put(mineralCode,mineralMineList);
        }
//        根据规则处理矿山数据
        for (String mineralCode:mineralDict.keySet()) {
//            得到该矿区对应的矿山数据集
            List<QueryMineInfoVO> mineralCodeMineList = (List<QueryMineInfoVO>)mineralDict.get(mineralCode);
            for (QueryMineInfoVO queryMineInfoVO:mineralCodeMineList) {
                if(StrUtil.startWith(Convert.toStr(queryMineInfoVO.getDjflbh()),"2")){
                    queryMineInfoVO.setMineObligeeType(LifeCycleConstant.mineTypes.get(1));
                }else if(StrUtil.startWith(Convert.toStr(queryMineInfoVO.getDjflbh()),"1")){
                    initDjflbhIsOne(queryMineInfoVO);
//                    如果是其他的，则看该矿区为1的 ,如果有许可证号
                }else{
                    QueryMineInfoVO queryMineInfoVOOne = null;
                    for (QueryMineInfoVO queryMineInfoVO1:mineralCodeMineList) {
                        if(StrUtil.startWith(Convert.toStr(queryMineInfoVO1.getDjflbh()),"1")){
                            queryMineInfoVOOne = queryMineInfoVO1;
                            break;
                        }
                    }
                    if(ObjectUtil.isNotNull(queryMineInfoVOOne)){
                        queryMineInfoVO.setMineObligeeType(initDjflbhIsOne(queryMineInfoVOOne));;
                    }else{
                        queryMineInfoVO.setMineObligeeType(LifeCycleConstant.mineTypes.get(0));
                    }

                }
                result.add(queryMineInfoVO);
            }
        }

        return result;
    }

    /*public List<MineVO> getKsExportTypMineVO(List<MineVO> mineVOS){
        List<MineVO> result = CollUtil.newArrayList();
        List<String> mineralCodes = mineVOS.stream().map(ele->ele.getKqbh()).distinct().collect(Collectors.toList());
//        矿区字典，key为矿区码，value为该矿区对应的矿山
        Dict mineralDict = Dict.create();
        for (String mineralCode:mineralCodes) {
            List<MineVO> mineralMineList = CollUtil.newArrayList();
            for (MineVO mineVO:mineVOS) {
                if(StrUtil.equals(mineralCode, mineVO.getKqbh())){
                    mineralMineList.add(mineVO);
                }
            }
            mineralDict.put(mineralCode,mineralMineList);
        }
//        根据规则处理矿山数据
        for (String mineralCode:mineralDict.keySet()) {
//            得到该矿区对应的矿山数据集
            List<MineVO> mineralCodeMineList = (List<MineVO>)mineralDict.get(mineralCode);
            for (MineVO mineVO:mineralCodeMineList) {
                if(StrUtil.startWith(Convert.toStr(mineVO.getDjflbh()),"2")){
                    mineVO.setMineObligeeType(LifeCycleConstant.mineTypes.get(1));
                }else if(StrUtil.startWith(Convert.toStr(mineVO.getDjflbh()),"1")){
                    initMineVODjflbhIsOne(mineVO);
//                    如果是其他的，则看该矿区为1的 ,如果有许可证号
                }else{
                    MineVO queryMineVOOne = null;
                    for (MineVO mineVO1:mineralCodeMineList) {
                        if(StrUtil.startWith(Convert.toStr(mineVO1.getDjflbh()),"1")){
                            queryMineVOOne = mineVO1;
                            break;
                        }
                    }
                    if(ObjectUtil.isNotNull(queryMineVOOne)){
                        mineVO.setMineObligeeType(initMineVODjflbhIsOne(queryMineVOOne));;
                    }else{
                        mineVO.setMineObligeeType(LifeCycleConstant.mineTypes.get(0));
                    }

                }
                result.add(mineVO);
            }
        }

        return result;
    }
*/


    private String initDjflbhIsOne(QueryMineInfoVO queryMineInfoVO){
        if(StrUtil.isNotBlank(queryMineInfoVO.getXkzh())){
            queryMineInfoVO.setMineObligeeType(LifeCycleConstant.mineTypes.get(2));
            return LifeCycleConstant.mineTypes.get(2);
        }else{
            queryMineInfoVO.setMineObligeeType(LifeCycleConstant.mineTypes.get(0));
            return LifeCycleConstant.mineTypes.get(0);
        }
    }

    /*private String initMineVODjflbhIsOne(MineVO mineVO){
        if(StrUtil.isNotBlank(mineVO.getXkzh())){
            mineVO.setMineObligeeType(LifeCycleConstant.mineTypes.get(2));
            return LifeCycleConstant.mineTypes.get(2);
        }else{
            mineVO.setMineObligeeType(LifeCycleConstant.mineTypes.get(0));
            return LifeCycleConstant.mineTypes.get(0);
        }
    }*/

    /**
     * 检查矿山信息列表中是否存在该id对应的矿山
     * @param queryMineInfoVOS
     * @param id
     * @return
     */
    private Boolean checkQuerymfHas(List<QueryMineInfoVO> queryMineInfoVOS,String id){
        return CollUtil.contains(queryMineInfoVOS,ele->{
            return StrUtil.equals(ele.getPkid(),id);
        });
    }

//    检查坐标是否是经纬度坐标
    private boolean checkCoorIs84(String x,String y){
        return checkNumDigit(x,2) && checkNumDigit(y,3);
    }

    @Override
    public String getQueryLifeCycleApplySQL(String msTableAs, Integer regClassCode) {
        return "substring("+msTableAs+".reg_class_code::varchar,length("+msTableAs+".reg_class_code::varchar)-1,2) = '"
                +getRegClassCodeSuffixTwo(regClassCode)+"'";
    }

    @Override
    public String getQueryLifeCycleApplyLikeSQL(String msTableAs, Integer regClassCode,String keyword,String direction) {
        String likeStr = "";
        if(StrUtil.equals(direction,"left")){
            likeStr = "'"+keyword+"%'";
        }else if(StrUtil.equals(direction,"right")){
            likeStr = "'%"+keyword+"'";
        }else{
            likeStr = "'%"+keyword+"%'";
        }

        return msTableAs+".reg_class_code::varchar like "+likeStr;
    }



    @Override
    public List<QueryMineStoreVO> queryLCNodeList(QueryMineStorePO queryMineStorePO,boolean isQueryCoor) {

        List<QueryMineStoreVO> listAll = CollUtil.newArrayList();

/*//        如果是
        if(queryMineStorePO.getRegClassCode() == 1000){
            regs.add(queryMineStorePO.getRegClassCode());
        }else{
            CollUtil.addAll(regs,CollUtil.newArrayList(queryMineStorePO.getRegClassCode(),1000));
        }
        if(!StrUtil.startWith(Convert.toStr(queryMineStorePO.getRegClassCode()),"3")){
            if(queryMineStorePO.getRegClassCode() != 1000){
                regs.add(3001);
            }
        }

        if(!StrUtil.startWith(Convert.toStr(queryMineStorePO.getRegClassCode()),"4")){
            if(queryMineStorePO.getRegClassCode() != 1000){
                regs.add(4001);
            }
        }*/
        List<Integer> regs = this.getQueLcRegClassCodes(queryMineStorePO.getRegClassCode());
        if(CollUtil.size(regs)!=0){
            for (Integer reg:regs) {
                queryMineStorePO.setRegClassCode(reg);
                CollUtil.addAll(listAll, queryLifeCycleMineList(queryMineStorePO,false));
            }
        }else{
            /*if(CollUtil.getFirst(regs)==1000){
                CollUtil.addAll(listAll, queryLifeCycleMineList(queryMineStorePO,true));
            }else{
                queryMineStorePO.setRegClassCode(CollUtil.getFirst(regs));
                CollUtil.addAll(listAll, queryLifeCycleMineList(queryMineStorePO,false));
            }*/
        }


        CollUtil.sortByProperty(listAll,"storeDate");
        if(CollUtil.size(listAll)!=0){
            initFirstPedigee(listAll);
        }

        if(StrUtil.isNotBlank(queryMineStorePO.getStoreDetailStr())){
            initYearReporetStore(listAll);
        }
        if(isQueryCoor){
            initLcCoor(listAll);
        }



        return this.handerMerge(listAll);
    }

    /**
     * 初始化生命周期节点坐标
     * @param queryMineStoreVOS
     */
    public void initLcCoor(List<QueryMineStoreVO> queryMineStoreVOS){
        for (QueryMineStoreVO queryMineStoreVO:queryMineStoreVOS) {
            LifeCycleMineStore lifeCycleMineStore = BeanUtil.copyProperties(queryMineStoreVO, LifeCycleMineStore.class);
            for (LifeCyclePedigee lifeCyclePedigee:queryMineStoreVO.getLifeCyclePedigeeList()) {
                lifeCyclePedigee.setMineCoordinateVO(getLcNodCoor(lifeCycleMineStore));
            }

        }
    }

    @Override
    public List<Integer> getQueLcRegClassCodes(Integer regClassCode) {
        String regClaCodeStr = Convert.toStr(regClassCode);
        if(regClassCode == 1000){
            return CollUtil.newArrayList(regClassCode);
        }

        if(StrUtil.startWith(regClaCodeStr,"2")){
            return CollUtil.newArrayList(1000,regClassCode);
        }

        if(StrUtil.startWith(regClaCodeStr,"3")){
            return CollUtil.newArrayList(1000,regClassCode,Convert.toInt("200"+StrUtil.subSuf(regClaCodeStr, StrUtil.length(regClaCodeStr))));
        }

        if(StrUtil.startWith(regClaCodeStr,"4")){
            return CollUtil.newArrayList(regClassCode);
        }

        return CollUtil.newArrayList();
    }

    /*protected List<List<QueryMineStoreVO>> groupBySD(List<QueryMineStoreVO> list){
        List<List<QueryMineStoreVO>> result = CollUtil.newArrayList();
        Map<LocalDate,Map<String,List<QueryMineStoreVO>>> map = list.stream().collect(Collectors.groupingBy(QueryMineStoreVO::getStoreDate,Collectors.groupingBy(QueryMineStoreVO::getReportName)));

        //遍历分组后的结果
        map.forEach((key1, mapEle) -> {
            mapEle.forEach((key2,qmsVOS)->{
                result.add(qmsVOS);
            });
        });
        return result;

    }*/


    @Override
    public List<QueryMineStoreVO> meterSameDateAndReportNameLCNodeList(List<QueryMineStoreVO> list) {
        List<QueryMineStoreVO> result = CollUtil.newArrayList();
        /*根据日期和节点名称分组*/
        List<List<QueryMineStoreVO>> groupQMSVOS =  CollUtil.groupByField(list,"storeDate");;
//
        /*过滤出相同日期和节点名称并且元素个数不是一个的分组项*/
        List<List<QueryMineStoreVO>> groupQMSVOSNotOne = groupQMSVOS.stream().filter(ele->CollUtil.size(ele)!=1).collect(Collectors.toList());
        if(CollUtil.isNotEmpty(groupQMSVOSNotOne)){
            /*得到相容日期和名称的日期列表*/
            List<LocalDate> notOneDateList =  groupQMSVOSNotOne.stream().map(ele->{
                QueryMineStoreVO queryMineStoreVO = CollUtil.getFirst(ele);
                return queryMineStoreVO.getStoreDate();
            }).collect(Collectors.toList());
            /*过滤出日期不重复的列表*/
            List<QueryMineStoreVO> uniqueList = list.stream().filter(ele->!CollUtil.contains(notOneDateList,ele.getStoreDate())).collect(Collectors.toList());
//            添加进结果集中
            CollUtil.addAll(result,uniqueList);

            /*不是唯一的节点列表*/
            List<QueryMineStoreVO> noUniqueList = CollUtil.newArrayList();

            /*相同日期的列表数据进行合并*/
            for (List<QueryMineStoreVO> groupQMSVONotOne:groupQMSVOSNotOne) {
                List<LifeCyclePedigee> allPed = CollUtil.newArrayList();
//                得到矿区的节点项
                List<QueryMineStoreVO> queryMineStoreVOS = groupQMSVONotOne.stream().filter(ele->StrUtil.equals(ele.getReportName(),CollUtil.get(LifeCycleConstant.flPedTypes,1))).collect(Collectors.toList());
//                List<QueryMineStoreVO> queryMineStoreVOS = groupQMSVONotOne.stream().filter(ele->ele.getRegClassCode() == 1000).collect(Collectors.toList());
                QueryMineStoreVO queryMineStoreVO = null;
                /*存在矿区的节点项目,否则取第一个*/
                if(CollUtil.isNotEmpty(queryMineStoreVOS)){
                    queryMineStoreVO = CollUtil.getFirst(queryMineStoreVOS);
                }else{
                    queryMineStoreVO = CollUtil.getFirst(groupQMSVONotOne);
                }

                /*将族谱合并*/
                for (QueryMineStoreVO queryMineStoreVO1:groupQMSVONotOne) {
                    CollUtil.addAll(allPed,queryMineStoreVO1.getLifeCyclePedigeeList());
                }

                queryMineStoreVO.setLifeCyclePedigeeList(allPed);

                noUniqueList.add(queryMineStoreVO);

            }
            CollUtil.addAll(result,noUniqueList);

            result = CollUtil.sortByProperty(result,"storeDate");

        }else{
            result = list;
        }

        return result;
    }

    @Override
    public List<QueryMineInfoVO> fromTKQCopyToWKQ(List<QueryMineInfoVO> queryMineInfoVOS) {

        List<QueryMineInfoVO> result = CollUtil.newArrayList();
//        遍历矿山信息列表
        for (QueryMineInfoVO queryMineInfoVO:queryMineInfoVOS) {
            queryMineInfoVO.setCpTkqToWkq(false);
            result.add(queryMineInfoVO);
//            如果是探矿权
            if(StrUtil.equals(CollUtil.getLast(LifeCycleConstant.mineTypes),queryMineInfoVO.getMineObligeeType())){
                try {
                    QueryMineInfoVO queryMineInfoVOCopy = (QueryMineInfoVO)queryMineInfoVO.clone();
                    queryMineInfoVOCopy.setPkid(queryMineInfoVOCopy.getPkid()+"_cp");
                    queryMineInfoVOCopy.setMineObligeeType(CollUtil.getFirst(LifeCycleConstant.mineTypes));
                    queryMineInfoVOCopy.setDjflbh(1000);
                    queryMineInfoVOCopy.setCpTkqToWkq(true);
                    result.add(queryMineInfoVOCopy);
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }

        return result;
    }

    private void initYearReporetStore(List<QueryMineStoreVO> queryMineStoreVOS){
        for (QueryMineStoreVO queryMineStoreVO:queryMineStoreVOS) {
            if(StrUtil.equals("年报",queryMineStoreVO.getReportName())){
                List<HisiKs> hisiKss = hisiKsService.lambdaQuery()
                        .eq(HisiKs::getDjflbh,queryMineStoreVO.getRegClassCode())
                        .eq(HisiKs::getKqbh,queryMineStoreVO.getMineralCode())
                        .eq(HisiKs::getNd,queryMineStoreVO.getYear()).list();
                if(CollUtil.size(hisiKss)!=0){
                    HisiKs hisiKs = CollUtil.getFirst(hisiKss);
                    if(CollUtil.size(queryMineStoreVO.getLifeCycleMineralMeaList())!=0){
                        LifeCycleMineralMea lifeCycleMineralMea = CollUtil.getFirst(queryMineStoreVO.getLifeCycleMineralMeaList());
                        lifeCycleMineralMea.setZs(Convert.toDouble(hisiKs.getKqzsl()));
                        lifeCycleMineralMea.setKx(Convert.toDouble(hisiKs.getKqkxl()));
                        lifeCycleMineralMea.setTm(Convert.toDouble(hisiKs.getKqtml()));
                        lifeCycleMineralMea.setKz(Convert.toDouble(hisiKs.getKqkzl()));
                        lifeCycleMineralMea.setTd(Convert.toDouble(hisiKs.getKqtdl()));
                        lifeCycleMineralMea.setDyl(Convert.toDouble(Convert.toDouble(hisiKs.getKqkcl())+Convert.toDouble(hisiKs.getKqssl())));
                    }
                }
            }

        }
    }

    /**
     * 检查是否是探矿权
     * @return
     */
    public boolean checkIsTKQ(QueryMineStoreVO queryMineStoreVO){
        if(ObjectUtil.isEmpty(queryMineStoreVO.getMineralCode())
            || ObjectUtil.isEmpty(queryMineStoreVO.getRegClassCode())
            || ObjectUtil.isEmpty(queryMineStoreVO.getYear())
        ){
            return false;
        }
        if(CollUtil.contains(LifeCycleConstant.firstPedRuleTypes, queryMineStoreVO.getReportName())){
            KclKs kclKs = kclKsService.lambdaQuery()
                    .eq(KclKs::getKqbh, queryMineStoreVO.getMineralCode())
                    .eq(KclKs::getDjflbh, queryMineStoreVO.getRegClassCode())
                    .eq(KclKs::getNd,queryMineStoreVO.getYear()).one();
            if(ObjectUtil.isNotEmpty(kclKs)){
                if(StrUtil.isNotBlank(kclKs.getXkzh()) || StrUtil.isNotBlank(kclKs.getKyqr())){
                    return true;
                }
            }else{
                return false;
            }
        }
        return false;

    }
    /**
     * 初始化第一个族谱
     * @return
     */
    @Override
    public void initFirstPedigee(List<QueryMineStoreVO> allQms){
        QueryMineStoreVO firstQMSVO = CollUtil.getFirst(allQms);
        if(CollUtil.size(firstQMSVO.getLifeCyclePedigeeList())==0){
//        if(CollUtil.size(firstQMSVO.getLifeCyclePedigeeList())==0 && CollUtil.contains(LifeCycleConstant.firstPedRuleTypes, firstQMSVO.getReportName())){
            firstQMSVO.setExporeType(Convert.toInt(LifeCycleConstant.mineTypeCodes.get(checkIsTKQ(firstQMSVO)?1:0)));

            LifeCyclePedigee lifeCyclePedigee = new LifeCyclePedigee();
            lifeCyclePedigee.setId(firstQMSVO.getMineralCode()+"_"+firstQMSVO.getRegClassCode()+"_"+ DateUtil.format(Convert.toDate(firstQMSVO.getStoreDate()), DatePattern.PURE_DATE_PATTERN));
            lifeCyclePedigee.setName(firstQMSVO.getReportName());
            lifeCyclePedigee.setExporeType(firstQMSVO.getExporeType());
            lifeCyclePedigee.setParentId(null);
            firstQMSVO.setLifeCyclePedigeeList(CollUtil.newArrayList(lifeCyclePedigee));

            for (int i = 1; i < CollUtil.size(allQms); i++) {
                QueryMineStoreVO queryMineStoreVO = CollUtil.get(allQms,i);
                if(CollUtil.size(queryMineStoreVO.getLifeCyclePedigeeList())!=0){
                    for (LifeCyclePedigee lifeCyclePedigee1:queryMineStoreVO.getLifeCyclePedigeeList()) {
                        lifeCyclePedigee1.setParentId(lifeCyclePedigee.getId());
                    }
                    break;
                }
            }

        }else{

        }
    }

   /* public void initFirstPedigee(List<QueryMineStoreVO> allQms){
        QueryMineStoreVO firstQMSVO = CollUtil.getFirst(allQms);
        firstQMSVO.setExporeType(Convert.toInt(LifeCycleConstant.mineTypeCodes.get(0)));
        if(CollUtil.size(firstQMSVO.getLifeCyclePedigeeList())==0){
            LifeCyclePedigee lifeCyclePedigee = new LifeCyclePedigee();
            lifeCyclePedigee.setId(firstQMSVO.getMineralCode()+"_"+firstQMSVO.getRegClassCode()+"_"+ DateUtil.format(Convert.toDate(firstQMSVO.getStoreDate()), DatePattern.PURE_DATE_PATTERN));
            lifeCyclePedigee.setParentId(null);
            firstQMSVO.setLifeCyclePedigeeList(CollUtil.newArrayList(lifeCyclePedigee));
            for (int i = 1; i < CollUtil.size(allQms); i++) {
                QueryMineStoreVO queryMineStoreVO = CollUtil.get(allQms,i);
                if(CollUtil.size(queryMineStoreVO.getLifeCyclePedigeeList())!=0){
                    for (LifeCyclePedigee lifeCyclePedigee1:queryMineStoreVO.getLifeCyclePedigeeList()) {
                        lifeCyclePedigee1.setParentId(lifeCyclePedigee.getId());
                    }
                    break;
                }
            }

        }
    }*/


    @Override
    public String getRegClassCodeSuffixTwo(Integer regClassCode){
        return StrUtil.sub(Convert.toStr(regClassCode),
                -2,
                StrUtil.length(Convert.toStr(regClassCode)));
    }

    @Override
    public List<QueryMineStoreVO> queryLifeCycleMineListRS(QueryMineStorePO queryMineStorePO) {
        List<QueryMineStoreVO> result = CollUtil.newArrayList();

        MPJLambdaWrapper<KclKs> wrapper = new MPJLambdaWrapper<KclKs>();
        wrapper.selectCollection(KclWjglb.class, QueryMineStoreRSVO::getKclWjglbList)
                .selectCollection(LifeCyclePedigee.class, QueryMineStoreRSVO::getLifeCyclePedigeeList)
                .selectAs(KclCb01Zh::getMc,QueryMineStoreRSVO::getReportName)
                .selectFunc(FuncEnum.CASE_STORE_VAL.getSql(),arg->arg.accept(KclCb06Kc::getJsldw, KclZycl::getKsl,KclZycl::getJsl),QueryMineStoreRSVO::getStoreVal)
//                .selectAs(QueryMineStoreRSVO)
                .leftJoin(KclZycl.class,on->
                        on.eq(KclZycl::getDjflbh,KclKs::getDjflbh)
                                .eq(KclZycl::getNd, KclKs::getNd)
                                .eq(KclZycl::getKqbh, KclKs::getKqbh)
                )
                .leftJoin(KclZlsc.class,on->
                                on.eq(KclZlsc::getDjflbh,KclKs::getDjflbh)
                                        .eq(KclZlsc::getNd, KclKs::getNd)
                                        .eq(KclZlsc::getKqbh, KclKs::getKqbh)

                .leftJoin(KclCb06Kc.class,KclCb06Kc::getKcdm, KclZycl::getKcdm)
                ).leftJoin(KclWjglb.class, KclWjglb::getZlscId, KclZlsc::getPkid)
                .leftJoin(LifeCyclePedigee.class, LifeCyclePedigee::getMineStoreId, LifeCycleMineStore::getId)
//                .leftJoin(LifeCycleMineExtend.class, LifeCycleMineExtend::getMineId,KclKs::getPkid)
                .leftJoin(KclCb01Zh.class,KclCb01Zh::getDm,KclZycl::getZycllb)
//                .leftJoin(KclKs.class, KclKs::getPkid,KclKs::getMineId)
                .eq(StrUtil.isNotBlank(queryMineStorePO.getOomrId()),KclKs::getKyqr, queryMineStorePO.getOomrId())
                .eq(StrUtil.isNotBlank(queryMineStorePO.getMineId()),KclKs::getPkid, queryMineStorePO.getMineId())
                .eq(ObjUtil.isNotNull(queryMineStorePO.getMineralType()),KclKs::getKsbh,queryMineStorePO.getMineralType())
                .le(ObjUtil.isNotNull(queryMineStorePO.getEndDate()),LifeCycleMineStore::getStoreDate,Convert.toLocalDateTime(queryMineStorePO.getEndDate()))
                .ge(ObjUtil.isNotNull(queryMineStorePO.getStartDate()),LifeCycleMineStore::getStoreDate, Convert.toLocalDateTime(queryMineStorePO.getStartDate()));
//                .in(CollUtil.isNotEmpty(queryMineStorePO.getReportIds()),KclZycl::getZycllb,queryMineStorePO.getReportIds());

//                .orderByAsc(LifeCycleMineStore::getStoreDate);
        List<QueryMineStoreRSVO> queryMineStoreRSVOS = kclKsMapper.selectJoinList(QueryMineStoreRSVO.class,wrapper);
        for (QueryMineStoreRSVO queryMineStoreRSVO:queryMineStoreRSVOS) {
            QueryMineStoreVO  queryMineStoreVO = BeanUtil.copyProperties(queryMineStoreRSVO,QueryMineStoreVO.class);
            queryMineStoreVO.setMineId(queryMineStoreRSVO.getPkid());


        }

        return result;
    }


    protected MPJLambdaWrapper<LifeCycleMineStore> onRegClassCodeWrapper(MPJLambdaWrapper<LifeCycleMineStore> wrapper,QueryMineStorePO queryMineStorePO){
//        如果是压覆类型
        if(StrUtil.startWith(Convert.toStr(queryMineStorePO.getRegClassCode()),"4")){
           return wrapper.and(wrapper1 ->
                        wrapper1.eq(LifeCycleMineStore::getRegClassCode, queryMineStorePO.getRegClassCode())
                        .or()
                        .eq(LifeCycleMineStore::getReportName,CollUtil.getFirst(LifeCycleConstant.crePedNodTypes))
                    );
        }else{
            return wrapper.eq(ObjectUtil.isNotEmpty(queryMineStorePO.getRegClassCode()),LifeCycleMineStore::getRegClassCode, queryMineStorePO.getRegClassCode());
        }
    }

    @Override
    public List<QueryMineStoreVO> queryLifeCycleMineList(QueryMineStorePO queryMineStorePO,boolean isQueryKQAll) {
        /*String applySQLStr = "";
        if(StrUtil.startWith(Convert.toStr(queryMineStorePO.getRegClassCode()),"3") || StrUtil.startWith(Convert.toStr(queryMineStorePO.getRegClassCode()),"4")){
            applySQLStr = getQueryLifeCycleApplyLikeSQL("t",queryMineStorePO.getRegClassCode(),StrUtil.startWith(Convert.toStr(queryMineStorePO.getRegClassCode()),"3")?"3":"4","left");
        }else if(queryMineStorePO.getRegClassCode()==1000){
            applySQLStr=null;
        }else{
            applySQLStr = getQueryLifeCycleApplySQL("t",queryMineStorePO.getRegClassCode());
        }*/
        MPJLambdaWrapper<LifeCycleMineStore> wrapper = getQueryLcBaseWrapper(queryMineStorePO,true,false);
        wrapper = onRegClassCodeWrapper(wrapper,queryMineStorePO);
//        wrapper = wrapper.eq(LifeCycleMineStore::getRegClassCode, queryMineStorePO.getRegClassCode());
        /*if(StrUtil.isNotBlank(applySQLStr)){
            if(!isQueryKQAll){
                wrapper = wrapper.apply(applySQLStr);
            }

        }else{
            if(!isQueryKQAll){
                wrapper = wrapper.eq(LifeCycleMineStore::getRegClassCode, queryMineStorePO.getRegClassCode());
            }
        }*/

        wrapper =wrapper.orderByAsc(LifeCycleMineStore::getStoreDate);
        List<QueryMineStoreVO> queryMineStoreVOS = lifeCycleMineStoreMapper.selectJoinList(QueryMineStoreVO.class, wrapper);

        queryMineStoreVOS.forEach(ele->{
            if(StrUtil.isNotBlank(queryMineStorePO.getMineralType())){
                ele.initMineralMea(Convert.toInt(queryMineStorePO.getMineralType()),queryMineStorePO.getTjdx(),queryMineStorePO.getKclb());
            }
            ele.initMineAreaBase64();
            ele.initOtherAll();
        });

        return handYearReport(queryMineStoreVOS);
    }

    public List<QueryMineStoreVO> handYearReport(List<QueryMineStoreVO> queryMineStoreVOS){
        return queryMineStoreVOS.stream().filter(ele->{
           if(!StrUtil.equals(LifeCycleConstant.yearReport,ele.getReportName())){
                return true;
           }

           if(ele.getYear()<=2022){
               return true;
           }

           if(StrUtil.equals(ele.getSpzt(),"5")){
               return true;
           }
            return false;
        }).collect(Collectors.toList());
    }


    public List<QueryMineStoreVO> queryLifeCycleMineListBak(QueryMineStorePO queryMineStorePO) {
        MPJLambdaWrapper<LifeCycleMineralMea> wrapper = new MPJLambdaWrapper<LifeCycleMineralMea>()
                .selectAll(LifeCycleMineStore.class)
                .selectAs(LifeCycleMineralMea::getStoreVal,QueryMineStoreVO::getStoreVal)
                .selectCollection(KclWjglb.class, QueryMineStoreVO::getKclWjglbList)
                .selectCollection(LifeCyclePedigee.class, QueryMineStoreVO::getLifeCyclePedigeeList)
                .leftJoin(LifeCycleMineStore.class, LifeCycleMineStore::getId, LifeCycleMineralMea::getMineStoreId)
                .leftJoin(KclZlsc.class,on->
                        on.eq(KclZlsc::getDjflbh,LifeCycleMineStore::getRegClassCode)
                                .eq(KclZlsc::getNd, LifeCycleMineStore::getYear)
                                .eq(KclZlsc::getKqbh, LifeCycleMineStore::getMineralCode)
                )
                .leftJoin(KclWjglb.class, KclWjglb::getZlscId, KclZlsc::getPkid)
                .leftJoin(LifeCyclePedigee.class, LifeCyclePedigee::getMineStoreId, LifeCycleMineStore::getId)
//                .leftJoin(KclKs.class, KclKs::getPkid,LifeCycleMineStore::getMineId)
                .eq(StrUtil.isNotBlank(queryMineStorePO.getOomrId()),KclKs::getKyqr, queryMineStorePO.getOomrId())
//                将这个矿山id换成，通过等级分类编号的后两位和矿区编号查询
                .eq(LifeCycleMineStore::getMineralCode,queryMineStorePO.getMineralCode())

//                .likeRight(ObjUtil.isNotEmpty(queryMineStorePO.getRegClassCode()),LifeCycleMineStore::getRegClassCode, StrUtil.sub(queryMineStorePO.getRegClassCode(),-2,StrUtil.length(Convert.toStr(queryMineStorePO.getRegClassCode()))))
//                .eq(StrUtil.isNotBlank(queryMineStorePO.getMineId()),LifeCycleMineStore::getMineId, queryMineStorePO.getMineId())
//                .eq(StrUtil.isNotBlank(queryMineStorePO.getMineId()),LifeCycleMineStore::getMineId, queryMineStorePO.getMineId())
                .eq(ObjUtil.isNotNull(queryMineStorePO.getMineralType()),LifeCycleMineralMea::getMineralType,Convert.toInt(queryMineStorePO.getMineralType()))
                .le(ObjUtil.isNotNull(queryMineStorePO.getEndDate()),LifeCycleMineStore::getStoreDate,Convert.toLocalDateTime(queryMineStorePO.getEndDate()))
                .in(CollUtil.isNotEmpty(queryMineStorePO.getReportNames()),LifeCycleMineStore::getReportName,queryMineStorePO.getReportNames())
                .eq(LifeCycleMineStore::getApproveStatus, 1)
                /*.apply("substring(t1.reg_class_code::varchar,length(t1.reg_class_code::varchar)-1,2) = '"
                        +StrUtil.sub(Convert.toStr(queryMineStorePO.getRegClassCode()),
                        -2,
                        StrUtil.length(Convert.toStr(queryMineStorePO.getRegClassCode())))+"'"
                )*/
                .apply(getQueryLifeCycleApplySQL("t",queryMineStorePO.getRegClassCode()))
                .orderByAsc(LifeCycleMineStore::getStoreDate);

        return lifeCycleMineralMeaMapper.selectJoinList(QueryMineStoreVO.class,wrapper);
    }


    @Override
    public List<QuerYearStoreVO> staticsYearStore(QueryYearStorePO queryYearStorePO) {

        return lifeCycleMapper.staticsYearStore(queryYearStorePO);
    }

    @Override
    public List<MineTypeVO> queryLifeCycleMineralTypeDict(QueryMineStorePO queryMineStorePO) {
        MPJLambdaWrapper<LifeCycleMineralMea> wrapper = new MPJLambdaWrapper<LifeCycleMineralMea>()
                .selectAs(KclCb06Kc::getKcdm, MineTypeVO::getKcdm)
                .selectAs(KclCb06Kc::getKcmc, MineTypeVO::getKcmc)
                .selectAs(KclCb06Kc::getJsdxmc, MineTypeVO::getJsdxmc)
//                .selectAs(KclCb06Kc::getKsldw, MineTypeVO::getUnit)
                .selectAs(KclCb06Kc::getKsldw, MineTypeVO::getKsldw)
                .selectAs(KclCb06Kc::getJsldw, MineTypeVO::getJsldw)
                .selectAs(KclCb06Kc::getTjdx, MineTypeVO::getTjdx)
                .selectAs(KclCb06Kc::getKclb, MineTypeVO::getKclb)
                .innerJoin(KclCb06Kc.class,on->
                        on.eq(KclCb06Kc::getKcdm, LifeCycleMineralMea::getMineralType)
                        .eq(KclCb06Kc::getTjdx, LifeCycleMineralMea::getTjdx)
//                        .eq(KclCb06Kc::getKclb, LifeCycleMineralMea::getKclb)
                )
                .innerJoin(LifeCycleMineStore.class, LifeCycleMineStore::getId, LifeCycleMineralMea::getMineStoreId)
                .eq(LifeCycleMineStore::getMineralCode, queryMineStorePO.getMineralCode())
//                .eq(LifeCycleMineStore::getMineId,mineId)
                /*.apply("substring(t2.reg_class_code::varchar,length(t2.reg_class_code::varchar)-1,2) = '"
                        +StrUtil.sub(Convert.toStr(queryMineStorePO.getRegClassCode()),
                        -2,
                        StrUtil.length(Convert.toStr(queryMineStorePO.getRegClassCode())))+"'"
                )*/
                .apply(getQueryLifeCycleApplySQL("t2",queryMineStorePO.getRegClassCode()))
                .distinct();
                List<MineTypeVO> mineTypeVOS = lifeCycleMineralMeaMapper.selectJoinList(MineTypeVO.class,wrapper);

                mineTypeVOS.forEach(ele->{
                    ele.initField();
                });
        return mineTypeVOS;

        /*MPJLambdaWrapper<LifeCycleMineStore> wrapper = new MPJLambdaWrapper<LifeCycleMineStore>()
                .selectAs(KclCb06Kc::getKcdm, MineTypeVO::getCodeInt)
                .selectAs(KclCb06Kc::getKcmc, MineTypeVO::getName)
                .selectAs(KclCb06Kc::getKsldw, MineTypeVO::getUnit)
                .leftJoin(KclCb06Kc.class,KclCb06Kc::getKcdm, LifeCycleMineStore::getMineralType)
                .eq(LifeCycleMineStore::getMineId,mineId).distinct();
        return lifeCycleMineStoreMapper.selectJoinList(MineTypeVO.class,wrapper);*/
    }

    @Override
    public long queryMineCount(KclKs kclKs){
        return kclKsMapper.selectJoinCount(getQueryMineWrapper(kclKs));
    }

    private MPJLambdaWrapper<KclKs> getQueryMineWrapper(KclKs kclKs){
        return new MPJLambdaWrapper<KclKs>()
                .selectAll(KclKs.class)
                .selectAs(LifeCycleMineExtend::getX4326, MineVO::getMineCenter84X)
                .selectAs(LifeCycleMineExtend::getY4326, MineVO::getMineCenter84Y)
                .leftJoin(LifeCycleMineExtend.class,on->
                        on.eq(LifeCycleMineExtend::getMineralCode,KclKs::getKqbh)
                                .eq(LifeCycleMineExtend::getRegClassCode, KclKs::getDjflbh)
                                .eq(LifeCycleMineExtend::getYear, KclKs::getNd)

                );
    }

    @Override
    public Page<MineVO> queryMineList(KclKs kclKs) {
        Page<MineVO> page = kclKsMapper.selectJoinPage(new Page<>(kclKs.getCurrentPageNum(),kclKs.getPageSize()),MineVO.class
                ,getQueryMineWrapper(kclKs)
        );

//        List<KclKs> kclKsList = kclKsService.queryKclKsList(kclKs);
        /*if(StrUtil.isNotBlank(kclKs.getXkzh())){
            mineVOS = CollUtil.newArrayList(CollUtil.getFirst(mineVOS));
        }*/

        /*for (KclKs kclKsItem:kclKsList) {
            MineVO mineVO = BeanUtil.copyProperties(kclKsItem,MineVO.class);
            if(StrUtil.isNotBlank(mineVO.getZxdxzb()) && StrUtil.isNotBlank(mineVO.getZxdyzb())){
                if(checkNumDigit(Convert.toStr(mineVO.getZxdxzb()),2) && checkNumDigit(Convert.toStr(mineVO.getZxdyzb()),3)){
                    mineVO.setMineCenter84X(Convert.toDouble(mineVO.getZxdxzb()));
                    mineVO.setMineCenter84Y(Convert.toDouble(mineVO.getZxdyzb()));
                }else{
                    List<Double> xyCoor= coordinateTranUtil.get84Coordinates(mineVO.getZxdxzb(),mineVO.getZxdyzb());
                    mineVO.setMineCenter84X(CollUtil.getFirst(xyCoor));
                    mineVO.setMineCenter84Y(CollUtil.getLast(xyCoor));
                }
            }
            result.add(mineVO);
        }*/
        return page;
    }

    @Override
    public Page<QueryMineInfoVO> queryMineListPage(QueryMineListPO queryMineListPO) {
        List<Integer> areaCodes = CollUtil.newArrayList();
        if(!StrUtil.equals(LifeCycleConstant.provinceName, queryMineListPO.getRoleType()) && !StrUtil.equals(LifeCycleConstant.oomrName, queryMineListPO.getRoleType())){
            QueryAdministractiveRegionPO queryAdministractiveRegionPO = new QueryAdministractiveRegionPO();
            queryAdministractiveRegionPO.setXzqdm(Convert.toInt(queryMineListPO.getXzqdm()));
            queryAdministractiveRegionPO.setRoleType(queryMineListPO.getRoleType());
            List<KclCb08Xzqh> kclCb08Xzqhs = getXZQList(queryAdministractiveRegionPO);
            areaCodes = kclCb08Xzqhs.stream().map(ele->ele.getDm()).collect(Collectors.toList());
        }else if(StrUtil.equals(LifeCycleConstant.oomrName, queryMineListPO.getRoleType())){
            areaCodes.add(queryMineListPO.getXzqdm());
        }


//        List<MineVO> result = CollUtil.newArrayList();
        Page<QueryMineInfoVO> page = kclKsMapper.selectJoinPage(new Page<>(queryMineListPO.getCurrentPageNum(),queryMineListPO.getPageSize())
                , QueryMineInfoVO.class
                ,new MPJLambdaWrapper<KclKs>()
                .selectAll(KclKs.class)
                .selectAs(KclKs::getPkid, QueryMineInfoVO::getMineStoreId)
                .selectAs(KclZb::getKqzb, QueryMineInfoVO::getDmlCoor)
                .selectAs(KclZb::getJszb, QueryMineInfoVO::getCalCoor)
                .selectAs(KclZb::getDlzb, QueryMineInfoVO::getMomCoor)
                .leftJoin(KclZb.class,"kclZb",on->
                        on.eq(KclZb::getNd,KclKs::getNd)
                                .eq(KclZb::getDjflbh,KclKs::getDjflbh)
                                .eq(KclZb::getKqbh, KclKs::getKqbh)
                )
                .leftJoin(KclClsp.class,on->
                        on.eq(KclClsp::getNd,KclKs::getNd)
                                .eq(KclClsp::getDjflbm, KclKs::getDjflbh)
                                .eq(KclClsp::getKqbh,KclKs::getKqbh)
                        )
                .innerJoin(GpKsMaxYear.class,"gp_ks_max_year", on->
                    on.eq(GpKsMaxYear::getMaxnd, KclKs::getNd)
                    .eq(GpKsMaxYear::getKqbh, KclKs::getKqbh)
                    .eq(GpKsMaxYear::getDjflbh, KclKs::getDjflbh)
//                    .apply(getKsDjflbhLastThreeLSQL("t"))
                )
                  .lt(KclKs::getNd,2022)
                  .or(r->r.gt(KclKs::getNd,2022).eq(KclClsp::getSpzt,"5"))
//                        审批通过的

                .eq(StrUtil.isNotBlank(queryMineListPO.getXkzh()), KclKs::getXkzh, queryMineListPO.getXkzh())
                .ne(KclKs::getSbbs,3)
                .in(CollUtil.size(areaCodes)!=0,KclKs::getXzqdm, areaCodes)
//                .eq(ObjectUtil.isNotEmpty(queryMineListPO.getNd()), KclKs::getNd, queryMineListPO.getNd())
        );


        /*for (QueryMineInfoVO queryMineInfoVO:page.getRecords()) {
            if(StrUtil.isNotBlank(queryMineInfoVO.getZxdxzb()) && StrUtil.isNotBlank(queryMineInfoVO.getZxdyzb())){
                if(checkNumDigit(Convert.toStr(queryMineInfoVO.getZxdxzb()),2) && checkNumDigit(Convert.toStr(queryMineInfoVO.getZxdyzb()),3)){
                    queryMineInfoVO.setMineCenter84X(Convert.toDouble(queryMineInfoVO.getZxdyzb()));
                    queryMineInfoVO.setMineCenter84Y(Convert.toDouble(queryMineInfoVO.getZxdxzb()));
                }else{
                    List<Double> xyCoor= coordinateTranUtil.get84Coordinates(queryMineInfoVO.getZxdxzb(),queryMineInfoVO.getZxdyzb());
                    queryMineInfoVO.setMineCenter84X(CollUtil.getFirst(xyCoor));
                    queryMineInfoVO.setMineCenter84Y(CollUtil.getLast(xyCoor));
                }
            }
        }*/
        initMineCool(page.getRecords(),false);
        page.setRecords(getKsExportType(page.getRecords()));
        return page;
    }

    @Override
    public List<MineVO> queryMineListTran(KclKs kclKs) {
        List<MineVO> result = CollUtil.newArrayList();

        List<KclKs> kclKsList = kclKsService.queryKclKsList(kclKs);
         if(StrUtil.isNotBlank(kclKs.getXkzh())){
             kclKsList = CollUtil.newArrayList(CollUtil.getFirst(kclKsList));
         }

        for (KclKs kclKsItem:kclKsList) {
            MineVO mineVO = BeanUtil.copyProperties(kclKsItem,MineVO.class);
            if(StrUtil.isNotBlank(mineVO.getZxdxzb()) && StrUtil.isNotBlank(mineVO.getZxdyzb())){
                if(checkNumDigit(Convert.toStr(mineVO.getZxdxzb()),2) && checkNumDigit(Convert.toStr(mineVO.getZxdyzb()),3)){
                    mineVO.setMineCenter84X(Convert.toDouble(mineVO.getZxdxzb()));
                    mineVO.setMineCenter84Y(Convert.toDouble(mineVO.getZxdyzb()));
                }else{
                    List<Double> xyCoor= coordinateTranUtil.get84Coordinates(mineVO.getZxdxzb(),mineVO.getZxdyzb());
                    mineVO.setMineCenter84X(CollUtil.getFirst(xyCoor));
                    mineVO.setMineCenter84Y(CollUtil.getLast(xyCoor));
                }
            }
            result.add(mineVO);
        }
        return result;
    }
/**
 * 检查数字位数
 * @param num 数字
 * @param howDigit 检查是几位
  */

    private boolean checkNumDigit(String num,int howDigit){
        boolean result = false;
        Integer numInt = Convert.toInt(num);
        if(StrUtil.length(Convert.toStr(numInt))== howDigit){
            result = true;
        }
        return result;
    }

    @Override
    public List<TreeNode<Integer>> getRegionNodes(List<KclCb08Xzqh> regionList){
        List<TreeNode<Integer>> regoionNodes = CollUtil.newArrayList();

        regionList.forEach(item->{
            TreeNode<Integer> treeNode = new TreeNode<>();
            treeNode.setId(item.getDm());
            treeNode.setParentId(item.getFdm());
            treeNode.setName(item.getMc()+"("+item.getDm()+")");

            Map<String,Object> extendMap = MapUtil.newHashMap();
            extendMap.put("params",item);

            treeNode.setExtra(extendMap);
            regoionNodes.add(treeNode);
        });

        return regoionNodes;
    }

    private List<KclCb08Xzqh> getParentXZQByChildrenId(Integer childrenId){
        List<KclCb08Xzqh> result = CollUtil.newArrayList();
        List<Dict> dictList = commonMapper.queryXZQParentsByChildrenId(Dict.create().set("childrenId", childrenId).set("tableName", "kcl_cb08_xzqh"));
        for (Dict item : dictList) {
            KclCb08Xzqh kclCb08Xzqh1 = new KclCb08Xzqh();
            kclCb08Xzqh1.setDm(item.getInt("dm"));
            kclCb08Xzqh1.setFdm(item.getInt("fdm"));
            kclCb08Xzqh1.setMc(item.getStr("mc"));
            result.add(kclCb08Xzqh1);
        }
        return result;
    }

    private List<KclCb08Xzqh> getChildrenXZQByParentId(Integer pId){
        List<KclCb08Xzqh> result = CollUtil.newArrayList();
        List<Dict> dictList = commonMapper.queryXZQChildrenByParentId(Dict.create().set("parentId", pId));
        for (Dict item : dictList) {
            KclCb08Xzqh kclCb08Xzqh1 = new KclCb08Xzqh();
            kclCb08Xzqh1.setDm(item.getInt("dm"));
            kclCb08Xzqh1.setFdm(item.getInt("fdm"));
            kclCb08Xzqh1.setMc(item.getStr("mc"));
            result.add(kclCb08Xzqh1);
        }
        return result;
    }

    private List<KclCb08Xzqh> getXZQList(QueryAdministractiveRegionPO queryAdministractiveRegionPO) {
        List<KclCb08Xzqh> result = CollUtil.newArrayList();
        if (StrUtil.equals(queryAdministractiveRegionPO.getRoleType(), LifeCycleConstant.countyName)) {
            result = getParentXZQByChildrenId(queryAdministractiveRegionPO.getXzqdm());
//            如果是市级，则需要查询子加上父
        }else if(StrUtil.equals(queryAdministractiveRegionPO.getRoleType(), LifeCycleConstant.cityName)){
            List<KclCb08Xzqh> parents = getParentXZQByChildrenId(queryAdministractiveRegionPO.getXzqdm());
            List<KclCb08Xzqh> childs = getChildrenXZQByParentId(queryAdministractiveRegionPO.getXzqdm());

            CollUtil.addAll(result,parents);
            CollUtil.addAll(result,childs);
            result = CollUtil.distinct(result,ele->ele.getDm(),true);
        }
        return result;
    }

    @Override
    public RegionMineTreeVO queryMineRegionTree(QueryAdministractiveRegionPO queryAdministractiveRegionPO) {
        RegionMineTreeVO result = new RegionMineTreeVO();
        KclCb08Xzqh kclCb08Xzqh = BeanUtil.copyProperties(queryAdministractiveRegionPO,KclCb08Xzqh.class);
        List<KclCb08Xzqh> regionList = CollUtil.newArrayList();
        if(StrUtil.equals(queryAdministractiveRegionPO.getRoleType(),LifeCycleConstant.provinceName)){
            regionList = kclCb08XzqhService.queryKclCb08XzqhList(new KclCb08Xzqh());
        }else{
            regionList = getXZQList(queryAdministractiveRegionPO);
        }


        List<Tree<Integer>> treeList = TreeUtil.build(getRegionNodes(regionList),kclCb08Xzqh.getFdm());
//        存在关键字,或者主要矿种，或者存在开采状态，或者存在矿山大小
        if(StrUtil.isNotBlank(queryAdministractiveRegionPO.getKeyword())
                || ObjectUtil.isNotEmpty(queryAdministractiveRegionPO.getZkcm())
                || CollUtil.size(queryAdministractiveRegionPO.getSizes())!=0
                || CollUtil.size(queryAdministractiveRegionPO.getExpStatus())!=0
        ){
            /*模糊匹配*/
            QueryMineInfoPO queryMineInfoPO = BeanUtil.copyProperties(queryAdministractiveRegionPO,QueryMineInfoPO.class);
//            QueryMineInfoPO queryMineInfoPO = new QueryMineInfoPO();
//            queryMineInfoPO.setYear(queryAdministractiveRegionPO.getYear());
//            queryMineInfoPO.setKeyword(kclCb08Xzqh.getKeyword());
            queryMineInfoPO.setAreaCodes(regionList.stream().map(ele->ele.getDm()).collect(Collectors.toList()));
            List<QueryMineInfoRetVO> queryMineInfoRetVOS = queryMineInfo(queryMineInfoPO);
            List<Integer> mineRegionCodes = CollUtil.newArrayList();
            List<String> regionNames = CollUtil.newArrayList();
            for (QueryMineInfoRetVO queryMineInfoRetVO:queryMineInfoRetVOS) {
                CollUtil.addAll(mineRegionCodes, queryMineInfoRetVO.getQueryMineInfoVOS().stream().map(ele->ele.getXzqdm()).collect(Collectors.toList()));
                CollUtil.addAll(regionNames,queryMineInfoRetVO.getQueryMineInfoVOS().stream().map(ele->ele.getRegionName()+"("+ele.getXzqdm()+")").collect(Collectors.toList()));
            }
            result.setRegionCodes(mineRegionCodes);
            result.setRegionNames(regionNames);
//            List<Tree<Long>> mineRegionTress = TreeUtil.build(getMineRegionTrees(result.getRegionCodes()),kclCb08Xzqh.getFdm());


            List<Tree<Integer>> trees = hutoolTreeExtend.getTreeFilter(treeList, queryAdministractiveRegionPO.getKeyword(), kclCb08Xzqh.getFdm(), result.getRegionCodes());
            result.setRegionTree(trees);


            result.setQueryMineInfoRetVOS(queryMineInfoRetVOS);

        }else{
            result.setRegionTree(treeList);
        }

        return result;
    }



    /**
     * 根据子id列表得到行政区的树节点
     * @param regionCodes
     * @return
     */
    private List<TreeNode<Integer>> getMineRegionTrees(List<Long> regionCodes){
//        List<Tree<Long>> result = CollUtil.newArrayList();

        List<Dict> parentDicts = commonMapper.queryParentsByChildrenIdExtend(regionCodes
                ,"kcl_cb08_xzqh","dm","fdm");
        List<KclCb08Xzqh> kclCb08Xzqhs = Convert.convert(new TypeReference<List<KclCb08Xzqh>>() {
        },parentDicts);

//        Console.log(parentDicts);

        return getRegionNodes(kclCb08Xzqhs);
    }
}

